package com.itheima.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class FileUploadController {
    @RequestMapping(value = "/fileupload")
    public String fileupload(MultipartFile[] file,HttpServletRequest request) throws IOException {
        if (file.length>0)
        for (MultipartFile multipartFile : file) {
            String[] split = multipartFile.getOriginalFilename().split(".", 2);
            String s = UUID.randomUUID().toString()+"."+split[1];

            String realPath = request.getServletContext().getRealPath("/images");
            multipartFile.transferTo(new File(realPath,s));
        }
        return "page.jsp";
    }

}
